package kyssion.leetcode.num201_250;

public class code238_除自身以外数组的乘积 {

    public static void main(String[] args) {
        int[] ooo = new code238_除自身以外数组的乘积().productExceptSelf(
                new int[]{
                        1, 2, 3, 4,3
                }
        );
    }

    public int[] productExceptSelf(int[] nums) {
        int[] ansArr = new int[nums.length];
        ansArr[0] = 1;
        for (int a = 1,before = 1; a < nums.length; a++) {
            if (a == 1) {
                ansArr[a] = nums[a - 1];
            } else {
                ansArr[a] = nums[a - 1] *ansArr[a - 1];
            }
        }
        for (int a = nums.length - 2, before = 1; a >= 0; a--) {
            if (a == nums.length - 2) {
                before = nums[a];
                nums[a] = nums[a + 1];
            } else {
                int p = nums[a + 1] * before;
                before = nums[a];
                nums[a] = p;
            }
        }
        for (int a = 0; a < ansArr.length; a++) {
            if (a == 0) {
                ansArr[a] = nums[a];
            } else if (a == ansArr.length - 1) {
                ansArr[a] = ansArr[a];
            } else {
                ansArr[a] = ansArr[a] * nums[a];
            }
        }
        return ansArr;
    }
}
